package com.ruoyi.framework.config;

import com.ruoyi.system.domain.CustomerInfo;
import com.ruoyi.system.domain.CustomerChat;
import com.ruoyi.system.mapper.CustomerInfoMapper;
import com.ruoyi.system.mapper.WaClientMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

@Service
@EnableRetry
public class ProcessingService {

    private static final Logger log = LoggerFactory.getLogger(ProcessingService.class);


    // 账户状态 &*&
    private static String SALEMAN_REDIS_TAG = "sale_man:";
    private static int ACCOUNT_OFFLINE = 0;  // 离线
    private static int ACCOUNT_ONLINE = 1;  // 在线
    private static int ACCOUNT_SENDING = 3;  // 发送中
    private static int ACCOUNT_PAUSED = 4;  // 暂停
    private static int ACCOUNT_SENT_SUCCESS = 5;  // 发送成功
    private static int ACCOUNT_BANNED = 6;  // 已封
    private static int ACCOUNT_UNBLOCK = 7;  // 提交审核
    private static int ACCOUNT_IN_CARE = 8;  // 正在养号
    private static int ACCOUNT_CARE_ENDED = 9;  // 养号结束
    private static int ACCOUNT_SEND_FAILED = 10;  // 发送失败
    private static int ACCOUNT_SYNC_BOOK = 11;  // 同步通讯录
    private static int ACCOUNT_SYNC_BOOK_FAILED = 12;  // 同步通讯录失败
    private static int ACCOUNT_SYNC_BOOK_SUCCESS = 13;  // 同步通讯录成功

    // 状态码标识 &*&
    private static String FANS_REDIS_TAG = "fans:";
    private static int FANS_NOT_SENT = 0;  // 未发送
    private static int FANS_INPUT_SUCCESS = 1;  // 录入成功
    private static int FANS_SENT_SUCCESS = 2;  // 发送成功
    private static int FANS_SENT_FAILED = 3;  // 发送失败
    private static int FANS_INPUT_FAILED = 4;  // 录入失败

    // 自动养号
    private static String SALEMAN_SEND_MESSAGE = "total";
    private static String SALEMAN_SEND_MESSAGE_NUMBER = "account";  // 发送消息数量
    private static String SALEMAN_SEND_MESSAGE_SUCCESS = "success";  // 发送消息成功

    // 通讯录同步
    private String SALEMAN_SYNC_BOOK = "sync";
    private String SALEMAN_SYNC_BOOK_NUMBER = "number";  // 发送消息数量
    private String SALEMAN_SYNC_BOOK_SUCCESS = "success";  // 发送消息成功

    @Autowired
    private CustomerInfoMapper waUserMapper;

    @Autowired
    private WaClientMapper waClientMapper;
    @Retryable(value = Exception.class, maxAttempts = 5, backoff = @Backoff(delay = 2000))
    public void process(String message) throws Exception {
        log.info("Received message: " + message);
//        sale_man:006_4_85255140083&*&6
        if (message.startsWith(SALEMAN_REDIS_TAG)){
            message = message.replace(SALEMAN_REDIS_TAG,"");
            log.info(message.trim());
            String[] mes = message.split("#");
            log.info(mes.length+"");
            if (mes.length == 2){
                log.info("sale_man 更新状态: " + message);
                CustomerInfo cusUser = new CustomerInfo();
                cusUser.setContactName(mes[0]);
                cusUser.setLinkStatus(mes[1]);
                waUserMapper.updateLinkStatusByContactName(cusUser);
            }else if(mes.length == 3){
                CustomerInfo cusUser = new CustomerInfo();
                cusUser.setContactName(mes[0]);
                if (mes[1].equals(SALEMAN_SEND_MESSAGE_NUMBER)){
                    cusUser.setSendNumber(mes[2]);
                    waUserMapper.updateLinkStatusByContactName(cusUser);
                } else if (mes[1].equals(SALEMAN_SYNC_BOOK)){
                    cusUser.setBatchNumber(mes[2]);
                    waUserMapper.updateLinkStatusByContactName(cusUser);
                }

            }

        }else if (message.startsWith(FANS_REDIS_TAG)){
            message = message.replace(FANS_REDIS_TAG,"");
            String[] mes = message.split("#");
            if (mes.length == 2){
                CustomerChat customerChat = new CustomerChat();
                customerChat.setStatus(Integer.valueOf(mes[1]));
                String number = mes[0].trim();
                number = number.replace(" ", "");
                customerChat.setClientNumber(number);
                waClientMapper.updateCustomerByClientNumber(customerChat);
//                toDayClientTotal
            } else if(mes.length == 3){
                CustomerChat customerChat = new CustomerChat();
                customerChat.setStatus(Integer.valueOf(mes[1]));
                customerChat.setConversationStage(Integer.valueOf(mes[1]));
                String number = mes[0].trim();
                number = number.replace(" ", "");
                customerChat.setClientNumber(number);
                waClientMapper.updateCustomerByClientNumber(customerChat);

            }
        }
    }


    public static void main(String[] args) {
        String message = "RRRE#11";
        String[] mes = message.split("#");
        System.out.print(mes.length+" ---");
    }

}


